package mypack;

public class Key {

    //init为初始64位密钥。生成16轮的密钥
    public static StringBuilder[] EncrykeyGenerator(StringBuilder init) {
        StringBuilder[] key = new StringBuilder[16];
        init = StaticChange.P1change(init);
        StringBuilder left = new StringBuilder(init.subSequence(0, 28));
        StringBuilder right = new StringBuilder(init.subSequence(28, 56));
        int[] move = new int[]{1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
        for(int i = 0; i < 16; i++) {
            left = StaticChange.leftMove(left, move[i]);
            right = StaticChange.leftMove(right, move[i]);
            StringBuilder tmp = new StringBuilder();
            key[i] = StaticChange.P2change(tmp.append(left).append(right));
        }
        return key;
    }

    public static StringBuilder[] DeciKeyGenerator(StringBuilder init) {
        StringBuilder[] key = new StringBuilder[16];
        init = StaticChange.P1change(init);
        StringBuilder left = new StringBuilder(init.subSequence(0, 28));
        StringBuilder right = new StringBuilder(init.subSequence(28, 56));
        int[] move = new int[]{0,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
        for(int i = 0; i < 16; i++) {
            left = StaticChange.rightMove(left, move[i]);
            right = StaticChange.rightMove(right, move[i]);
            StringBuilder tmp = new StringBuilder();
            key[i] = StaticChange.P2change(tmp.append(left).append(right));
        }
        return key;
    }

    public static void main(String[] args) {
        StringBuilder[] re = EncrykeyGenerator(new StringBuilder(args[0]));
        //System.out.println(re.toString());
        for(int i = 0; i < 16; i++)
            System.out.println(re[i]);
    }
}